package com.groupbuying.redpacket.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @ClassName SpringConfigTool
 * @Description: TODO   spring工具类
 * @Author srlg3
 * @Date 2019/11/19
 * @Version V1.0
 **/
@Component
public class SpringConfigTool implements ApplicationContextAware {

    private static ApplicationContext context = null;
    private static SpringConfigTool stools = null;

    public synchronized static SpringConfigTool init() {
        if (stools == null) {
            stools = new SpringConfigTool();
        }
        return stools;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        context = applicationContext;
    }

    /**
     * @MethodName: getBean
     * @Description: TODO 获取spring容器中对应的bean
     * @param clazz
     * @Return: T
     * @Author: srlg3
     * @Date: 2019/11/20
     */

    public synchronized static <T> T getBean(Class<T> clazz) {
        return (T) context.getBean(clazz);
    }

}